/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import javax.swing.*;

import java.beans.*;
import java.awt.*;
import java.io.File;
import java.net.URI;
import java.util.concurrent.CancellationException;

public class ImagePreview extends LoadingPanel
  		implements PropertyChangeListener {
 
	private static final long serialVersionUID = -8881183466971000729L;
	
	private Icon image;
	
	private JFileChooser fc;
	
	private FSImageLoader loader;
	
	private URI uri;

	public ImagePreview(JFileChooser fc) {
		this.fc = fc;
		setPreferredSize(new Dimension(100, -1));
		fc.addPropertyChangeListener(this);
		setIcon(null);
	}

	public Icon getIcon() {
		return image;
	}
	
	public void setIcon(Icon icon) {
		setAnimationStarted(icon == null && uri != null);
		image = icon;
	}

	public void propertyChange(PropertyChangeEvent e) {
		File[] files = fc.getSelectedFiles();
		if(files == null || files.length == 0) {
			setIcon(null);
			uri = null;
			repaint();
		} else {
			URI newUri = files[files.length - 1].toURI();
			if(newUri.equals(uri)) {
				return;
			}
			
			uri = newUri;
			setIcon(null);
			
			if(loader != null && !loader.isDone()) {
				loader.cancel(false);
				loader = null;
			}
			
			loader = new FSImageLoader(uri, getWidth(), getHeight());
			loader.execute();
			
			repaint();
		}
	}

	protected void paintComponent(Graphics g) {
		if (getIcon() != null) {
			int x = getWidth()/2 - getIcon().getIconWidth()/2;
			int y = getHeight()/2 - getIcon().getIconHeight()/2;

			if (y < 0) {
				y = 0;
			}

			if (x < 5) {
				x = 5;
			}
			getIcon().paintIcon(this, g, x, y);
		} else if(uri != null) {
			super.paintComponent(g);
		}
	}
	private class FSImageLoader extends ImageLoader {
		private static final long serialVersionUID = 4763520534816943205L;
		public FSImageLoader(URI uri, int width, int height) {
			super(uri, width, height);
		}
		@Override
		public void error(Throwable e, Icon icon) {
			if(!(e instanceof CancellationException)) {
				e.printStackTrace();
				putIcon(icon);
			}
		}
		@Override
		public void success(Icon icon) {
			putIcon(icon);
		}
		public void putIcon(Icon icon) {
			if(getLoadPath().equals(uri)) {
				setIcon(icon);
				repaint();
			}
		}
	}
}
